function createAsyncFunction(asyncFunction) {
    return function() {
        var result = asyncFunction.apply(this, arguments);
        return new Promise(function(resolve, reject) {
            return function handlePromise(state, value) {
                try {
                    var step = result[state](value), 
                    stepValue = step.value;
                } catch (error) {
                    return void reject(error);
                }
                if (!step.done) return Promise.resolve(stepValue).then(function(value) {
                    handlePromise("next", value);
                }, function(error) {
                    handlePromise("throw", error);
                });
                resolve(stepValue);
            }("next");
        });
    };
}

getApp();

var dataApi = require("../../provider/dataApi.js");

Page({
    data: {
        statusBarHeight: "",
        titleBarHeight: "",
        userInfo: {}
    },
    onLoad: function(options) {
        this.setData({
            statusBarHeight: getApp().globalData.statusBarHeight,
            titleBarHeight: getApp().globalData.titleBarHeight
        }), this.getUserInfo();
    },
    getUserInfo: function() {
        var pageInstance = this;
        dataApi.userinfo({
            uid: wx.getStorageSync("uid"),
            op: "getinfo"
        }).then(function(response) {
            console.log(response), pageInstance.setData({
                userInfo: response.data.info
            });
        }).catch(function(error) {});
    },
    goBack: function() {
        wx.navigateBack({
            delta: 1
        });
    },
    saveImgBtn: function() {
        var customerServiceImage = this.data.userInfo.customer_service;
        wx.showLoading({
            title: "保存中"
        }), this.uploadImg(customerServiceImage);
    },
    uploadImg: function(imageUrl) {
        var pageInstance = this;
        return createAsyncFunction(regeneratorRuntime.mark(function asyncFunction() {
            var imageInfo, filePath;
            return regeneratorRuntime.wrap(function(asyncFunction) {
                for (;;) switch (asyncFunction.prev = asyncFunction.next) {
                  case 0:
                    imageInfo = pageInstance, asyncFunction.next = 3, imageInfo.getImgInfo(imageUrl);

                  case 3:
                    filePath = asyncFunction.sent, console.log(filePath.path), wx.saveImageToPhotosAlbum({
                        filePath: filePath.path,
                        success: function(response) {
                            console.log(response), wx.hideLoading(), wx.showToast({
                                title: "保存成功",
                                icon: "none"
                            });
                        },
                        fail: function(error) {
                            console.log(error), wx.hideLoading(), "saveImageToPhotosAlbum:fail:auth denied" !== error.errMsg && "saveImageToPhotosAlbum:fail auth deny" !== error.errMsg || wx.showModal({
                                title: "提示",
                                content: "需要您授权保存相册",
                                showCancel: !1,
                                success: function() {
                                    wx.openSetting({
                                        success: function(settingData) {
                                            console.log("settingdata", settingData), wx.hideLoading(), settingData.authSetting["scope.writePhotosAlbum"] ? wx.showModal({
                                                title: "提示",
                                                content: "获取权限成功,再次点击保存图片即可保存图片",
                                                showCancel: !1
                                            }) : wx.showModal({
                                                title: "提示",
                                                content: "获取权限失败，将无法保存到相册哦~",
                                                showCancel: !1
                                            });
                                        }
                                    });
                                }
                            });
                        }
                    });

                  case 6:
                  case "end":
                    return asyncFunction.stop();
                }
            }, asyncFunction, pageInstance);
        }))();
    },
    getImgInfo: function(imageUrl) {
        return new Promise(function(resolve) {
            wx.getImageInfo({
                src: imageUrl,
                success: function(imageInfo) {
                    console.log(imageInfo), resolve(imageInfo);
                },
                fail: function(error) {
                    console.log(error);
                }
            });
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});